import { app } from '@/const/config'

export const StorageKey = {
  language: 'language'
}

const formatKey = key => app.prefix + '_' + key

export const setStorage = (key: string, data: string | { [key: string]: any }) => {
  window.localStorage.setItem(
    formatKey(key),
    typeof data === 'string' ? data : JSON.stringify(data)
  )
}

export const getStorage = <T>(key: string) => {
  const data = window.localStorage.getItem(formatKey(key))
  let res = data || ''
  if (data && data.includes('{')) {
    res = JSON.parse(data)
  }
  // @ts-ignore
  return res as T
}

export const removeStorage = (key: string) => {
  window.localStorage.removeItem(formatKey(key))
}
